package com.heliming.data.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.heliming.data.entity.User;
import com.heliming.data.service.WriterService;
import com.heliming.data.utils.ResultInfo;
import com.heliming.data.utils.ResultPage;

@RestController
@RequestMapping("/writer")
public class WriterController {
	
	@Autowired
	private WriterService writerService;
	
    @GetMapping("/index")
    public String index() {
        return "index";
    }
    
	@PostMapping("/insert")
	public String create(@RequestBody @Valid User user, BindingResult result){
		if (result.hasErrors()) {
			return ResultInfo.getValidationMessage(result);
		}
		String id = writerService.insert(user);
		return ResultPage.successToJsonString(id);
	}    

}
